package cn.edu.abtu.service.impl;

import cn.edu.abtu.dao.EduVideoDao;
import cn.edu.abtu.entity.EduChapter;
import cn.edu.abtu.dao.EduChapterDao;
import cn.edu.abtu.entity.EduVideo;
import cn.edu.abtu.entity.chapter.ChapterVo;
import cn.edu.abtu.entity.chapter.VideVo;
import cn.edu.abtu.exceptionhandler.GuliException;
import cn.edu.abtu.service.EduChapterService;
import cn.edu.abtu.vo.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class EduChapterServiceImpl implements EduChapterService {

    @Autowired
    private EduChapterDao eduChapterDao;

    @Autowired
    private EduVideoDao eduVideoDao;


    @Override
    public ResponseData addChapter(EduChapter eduChapter) {
        eduChapter.setGmtCreate(new Date());
        eduChapter.setGmtModified(new Date());
        int insert = eduChapterDao.insert(eduChapter);
        if (insert>0){
            return ResponseData.success("添加章节信息成功!",null);
        }else {
            return ResponseData.fail("添加章节信息失败!",null);
        }
    }

    @Override
    public ResponseData deleteChapterInfo(String chapterId) {
        // 1. 根据章节Id查询该章节的信息
        QueryWrapper<EduChapter> eduChapterQueryWrapper = new QueryWrapper<>();
        eduChapterQueryWrapper.eq("id",chapterId);
        EduChapter eduChapter = eduChapterDao.selectOne(eduChapterQueryWrapper);
        // 2.调用eduVideoDao查询该章节中是否存在小节eduVideo
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id",chapterId);
        Integer integer = eduVideoDao.selectCount(eduVideoQueryWrapper);
        if (integer == 0){ //章节中没有小节,可以直接删除
            int i = eduChapterDao.deleteById(chapterId);
            if (i>0){
                return ResponseData.success(eduChapter.getTitle()+"章节信息删除成功!",null);
            }else {
                return ResponseData.fail(eduChapter.getTitle()+"章节信息删除失败",null);
            }
        }else {
            return ResponseData.fail(eduChapter.getTitle()+"章节存在小节,请先删除小节内容",null);
        }
    }

    @Override
    public ResponseData getChapterVideo(String courseId) {
        //1.根据课程ID查询课程里边的所有章节
        QueryWrapper<EduChapter> queryWrapperChapter = new QueryWrapper();
        queryWrapperChapter.eq("course_id",courseId);
        queryWrapperChapter.orderByDesc("course_id");
        List<EduChapter> eduChapterList = eduChapterDao.selectList(queryWrapperChapter);

        //2.根据课程ID查询课程里边的所有小节
        QueryWrapper<EduVideo> queryWrapperVideo = new QueryWrapper();
        queryWrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoDao.selectList(queryWrapperVideo);

        //3.创建List集合用于封装最终数据
        List<ChapterVo> finalList = new ArrayList<>();

        //4.遍历查询章节eduChapterList集合进行封装
        for (EduChapter eduChapter : eduChapterList) {
            //把eduChapter对象赋值到chapterVo中
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapterVo存储到finalList
            finalList.add(chapterVo);
            //5.遍历查询小节eduVideoList集合进行封装
            List<VideVo> videVoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideoList) {
                //判断小节中的chapterId是否和章节的Id一致
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideVo videVo = new VideVo();
                    BeanUtils.copyProperties(eduVideo,videVo);
                    videVoList.add(videVo);
                }
            }
            chapterVo.setChildren(videVoList);
        }
        return ResponseData.success(finalList);
    }

    @Override
    public ResponseData getChapterInfo(String chapterId) {
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("id",chapterId);
        EduChapter eduChapter = eduChapterDao.selectOne(chapterQueryWrapper);
        if (eduChapter == null){
            throw  new GuliException(20001,"章节信息不存在");
        }
        return ResponseData.success("章节信息查询成功!",eduChapter);
    }

    @Override
    public ResponseData updateChapter(EduChapter eduChapter) {
        eduChapter.setGmtModified(new Date());
        int update = eduChapterDao.updateById(eduChapter);
        if (update>0){
            return ResponseData.success("修改章节信息成功!",null);
        }else {
            return ResponseData.fail("修改章节信息失败!",null);
        }
    }
}
